﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace XMLDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Write();
             Read();
        }

        private static void Write() {
            XmlDocument document = new XmlDocument();
            XmlElement rootNode = document.CreateElement("Root");
            XmlElement productionElem = CreateProduction(document);
            XmlElement itemElem = document.CreateElement("Items");
            itemElem.AppendChild(CreateItem(document));
            itemElem.AppendChild(CreateItem(document));
            rootNode.AppendChild(productionElem);
            rootNode.AppendChild(itemElem);
            document.AppendChild(rootNode);
            document.Save(@"D:\out.xml");
        }
        private static XmlElement CreateItem(XmlDocument document) {
            XmlElement item = document.CreateElement("Item");
            XmlAttribute nameAttr = document.CreateAttribute("Name");
            nameAttr.Value = "A";
            XmlAttribute qtyArr = document.CreateAttribute("Qty");
            qtyArr.Value = XmlConvert.ToString(1);
            item.Attributes.Append(nameAttr);
            item.Attributes.Append(qtyArr);
            return item;
        }

        private static XmlElement CreateProduction(XmlDocument document)
        {
            XmlElement item = document.CreateElement("Production");
            XmlAttribute nameAttr = document.CreateAttribute("Name");
            nameAttr.Value = "F";
            XmlAttribute valueArr = document.CreateAttribute("Value");
            valueArr.Value = XmlConvert.ToString(1);
            item.Attributes.Append(nameAttr);
            item.Attributes.Append(valueArr);
            return item;
        }

        private static void Read()
        {
            //string schemaFileName = @"E:\out.xsd";
            //string targetNameSpace = "www.xing.yao.guang.com";
            //XmlReaderSettings settings = new XmlReaderSettings();
            //settings.Schemas.Add(targetNameSpace, schemaFileName);
            //settings.ValidationType = ValidationType.Schema;
            string xmlFileName = @"D:\out.xml";
            try {
                //读取xml文件
                using (XmlReader reader = XmlReader.Create(xmlFileName))
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(reader);
                    const string itemXPath = "/Root/Items/Item";
                    XmlNodeList itemsNodeList = document.DocumentElement.SelectNodes(itemXPath);
                    foreach (XmlNode node in itemsNodeList)
                    {
                        string name = node.Attributes["Name"].Value;
                        int qty = XmlConvert.ToInt32(node.Attributes["Qty"].Value);
                    }
                    const string productionXPath = "/Root/Production";
                    XmlNode productionNode = document.DocumentElement.SelectSingleNode(productionXPath);
                    
                    string pName = productionNode.Attributes["Name"].Value;
                    int pValue = XmlConvert.ToInt32(productionNode.Attributes["Value"].Value);
                    
                }
            } catch(Exception e) {
                throw new Exception(e.Message);
            }
        }
    }
}
